﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizzReports;
using System.Data;

namespace OutboxStatsReport
{
    class OutboxStatsReportCommand : IBizzCommand
    {
        public DateTime fromDate { get; set; }
        public DateTime toDate { get; set; }
        public DataTable reportData { get; set; }
        public string selectedOutbox { get; set; }
        public string selectedMessageId { get; set; }
        

        public OutboxStatsReportCommand()
        {
             fromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
             toDate = DateTime.Now;
             selectedOutbox = "";
             reportData = new DataTable();
             reportData.Columns.Add("Mensaje");
             reportData.Columns.Add("Bandeja de entrada");
             reportData.Columns.Add("Fecha");
        }

        public void Update()
        {
            if (ReportQuery == null)
                ReportQuery = new BizzQuery();
            DateTime toDatePlusOne = toDate;
            toDatePlusOne = toDatePlusOne.AddDays(1);
            ReportQuery.queryString = "select m.id, Title as 'Mensaje', Inbox as 'Bandeja de entrada', MsgDate as 'Fecha' " +
                                      " from Messages m " +
                                      " inner join outboxeslog o on m.id = o.MessageEntity_id where OutboxName = '" + selectedOutbox + "' " +
                                      " and MsgDate >= '" + fromDate.ToString("yyyy/MM/dd") + "' And MsgDate <= '" + toDatePlusOne.ToString("yyyy/MM/dd") + "'";
            reportData = ReportQuery.ExecuteQuery().Tables[0];
        }

        public string[] getOutboxes()
        {
            if (ReportQuery == null)
                ReportQuery = new BizzQuery();
            ReportQuery.queryString = "select distinct OutBoxName as 'Bandeja de salida' from outboxeslog";

            var cols = (from r in ReportQuery.ExecuteQuery().Tables[0].AsEnumerable()
                       select r[0].ToString()).ToArray<String>();
            return cols;
        }

        public DataTable getMessageAttributes()
        {
            if (ReportQuery == null)
                ReportQuery = new BizzQuery();
            ReportQuery.queryString = "select Name as 'Nombre', Type as 'Tipo', Value as 'Valor'" +
                                      " from dbo.MessageAttribute where MessageEntity_id = " + selectedMessageId;
            return ReportQuery.ExecuteQuery().Tables[0];
        }


        #region IBizzCommand Members

        public BizzQuery ReportQuery
        {
            get;
            set;
        }

        public string Title
        {
            get {return "Mensajes por bandeja de salida"; }
        }

        public BizzReportTemplate showReportPanel()
        {
            return new OutboxStatsReport(this);
        }

        #endregion
    }
}
